package com.andnux.android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.andnux.android.adapter.OnItemClickListener;
import com.andnux.android.adapter.UserWorksAdapter;
import com.andnux.android.data.PageData;
import com.andnux.android.data.SystemUsers;
import com.andnux.android.data.Works;
import com.andnux.android.databinding.ActivityUserWorksBinding;
import com.andnux.android.network.DesignCloudApi;
import com.andnux.android.network.NotNullConsumer;
import com.andnux.android.network.Result;
import com.andnux.android.network.RetrofitClient;
import com.andnux.android.utils.ImageLoader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class UserWorksActivity extends BaseActivity<ActivityUserWorksBinding> {

    private UserWorksAdapter adapter;
    private int current = 0;
    private static final int size = 10;
    private final List<Works> list = new ArrayList<>();
    private SystemUsers users;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        users = (SystemUsers) getIntent().getSerializableExtra("users");
        binding.toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        ImageLoader.load(users.getAvatar(), binding.hedImage);
        binding.nickname.setText(users.getNickname());
        adapter = new UserWorksAdapter();
        adapter.setOnItemClickListener(new OnItemClickListener<com.andnux.android.databinding.ItemWorksUserBinding>() {
            @Override
            public void onItemClick(com.andnux.android.databinding.ItemWorksUserBinding view, int position, long id) {
                Intent intent = new Intent(UserWorksActivity.this, WorksDetailsActivity.class);
                intent.putExtra("works", list.get(position));
                startActivity(intent);
            }
        });
        binding.smart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                current++;
                loadData();
            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                current = 0;
                list.clear();
                loadData();
            }
        });
        if (users != null) {
            String role = users.getRole();
            if ("1".contentEquals(role)) {
                binding.title.setText("需求集");
            } else {
                binding.title.setText("作品集");
            }
        }
        binding.content.setAdapter(adapter);
        loadData();
    }

    private void loadData() {
        DesignCloudApi api = RetrofitClient.getInstance().getServerApi();
        String lx = "";
        if (users != null) {
            String role = users.getRole();
            if ("1".contentEquals(role)) {
                lx = "1";
            } else {
                lx = "0";
            }
            addDisposable(api.worksPage(current, size, lx, "",
                    String.valueOf(users.getId()), 1)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new NotNullConsumer<Result<PageData<List<Works>>>>() {
                        @Override
                        public void onSuccess(Result<PageData<List<Works>>> result) {
                            List<Works> works = result.getData().getList();
                            list.addAll(works);
                            adapter.setNewData(list);
                            if (works.size() < size) {
                                binding.smart.finishLoadMoreWithNoMoreData();
                            } else {
                                binding.smart.finishLoadMore();
                            }
                            binding.smart.finishRefresh();
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            binding.smart.finishLoadMoreWithNoMoreData();
                            binding.smart.finishRefresh();
                            if (throwable != null) {
                                Toast.makeText(UserWorksActivity.this, throwable.getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        }
                    }));
        }

    }
}